/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResourceWeapon extends GameResourceAttributeAttack
{
	public GameResourceWeapon()
	{	
		super();
	}
	
	public GameResourceWeapon(Parcel in)
	{
		super(in);
	}
	
	public static final Parcelable.Creator<GameResourceWeapon> CREATOR = new Parcelable.Creator<GameResourceWeapon>() 
	{
		public GameResourceWeapon createFromParcel(Parcel in) 
		{
		    return new GameResourceWeapon(in);
		}
		
		public GameResourceWeapon[] newArray(int size) 
		{
		    return new GameResourceWeapon[size];
		}
	};
	
	@Override
	public int getAbilityType()
	{
		int ability = super.getAbilityType();
		
		if (isMelee() && isFinessable())
			ability = GameAbilities.Dexterity;
		
		return ability;
	}
	
	public String getDamageOffHand()
	{
		return getDamage(0, true, false);
	}
	
	public String getDamageTwoHanded()
	{
		return getDamage(0, false, true);
	}
	
	public String getDamageTwoWeapon(boolean secondaryIsLight, boolean offhand)
	{
		return getDamage(0, offhand, false);
	}
	
	public String getDamageTwoWeapon()
	{
		return getDamageTwoWeapon(false, false);
	}
	
	public String getDamageTwoWeaponOffHand()
	{
		return getDamageTwoWeapon(false, true);
	}
	
	public String getDamageTwoWeaponSecondaryLight()
	{
		return getDamageTwoWeapon(true, false);
	}
	
	public int getValueOffHand()
	{
		return getValue();
	}
	
	public String getValueOffHandIterative()
	{
		return getValueIterative();
	}
	
	public int getValueRange(int range)
	{
		return getValueAdditional() + (range * -2);
	}
	
	public String getValueRangeIterative(int range)
	{
		return interativeAttackBonus(getValueRange(range));
	}
	
	public int getValueTwoHanded()
	{
		return getValue();
	}
	
	public String getValueTwoHandedIterative()
	{
		return getValueIterative();
	}
	
	public int getValueTwoWeapon(boolean secondaryIsLight, boolean offhand)
	{
		int modifier = 0;
		if (!secondaryIsLight)
		{
			modifier = -6;
			if (offhand)
			{
				modifier = -10;
				if (isLight())
					modifier = -8;
			}
		}
		else
		{
			modifier = -4;
			if (isLight())
				modifier = -8;
		}
		
		if (offhand)
			modifier -= this._twoWeaponSecondary;
		else 
			modifier -= this._twoWeaponPrimary;
		
		return getValueAdditional() + modifier;
	}
	
	public int getValueTwoWeapon()
	{
		return getValueTwoWeapon(false, false);
	}
	
	public int getValueTwoWeaponOffHand()
	{
		return getValueTwoWeapon(false, true);
	}
	
	public int getValueTwoWeaponSecondaryLight()
	{
		return getValueTwoWeapon(true, false);
	}
	
	public String getValueTwoWeaponIterative()
	{
		return getValueTwoWeaponIterative(false, false);
	}
	
	public String getValueTwoWeaponOffHandIterative()
	{
		return getValueTwoWeaponIterative(false, true);
	}
	
	public String getValueTwoWeaponSecondaryLightIterative()
	{
		return getValueTwoWeaponIterative(true, false);
	}
	
	public String getValueTwoWeaponIterative(boolean secondaryIsLight, boolean offhand)
	{
		return interativeAttackBonus(getValueTwoWeapon(secondaryIsLight, offhand));
	}
	
	@Override
	public boolean isLight()
	{
		return _light == 1;
	}
	
	@Override
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		super.load(data);
		
		if (data == null)
			return null;

		_melee = getJSONInt(data, "melee");
		_ranged = getJSONInt(data, "ranged");
		_light = getJSONInt(data, "light");
		_twoWeaponPrimary = getJSONInt(data, "twoWeaponPrimary");
		_twoWeaponSecondary = getJSONInt(data, "twoWeaponSecondary");
	    
	    return data;
	}
	
	@Override
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = super.save();

	    data.put("melee", _melee);
	    data.put("ranged", _ranged);
	    data.put("light", _light);
	    data.put("twoWeaponPrimary", _twoWeaponPrimary);
	    data.put("twoWeaponSecondary", _twoWeaponSecondary);
	    
	    return data;
	}
	
	public void setLight(int value)
	{
		_light = value;
	}
	
	public void setMelee(int value)
	{
		_melee = value;
	}
	
	public void setRanged(int value)
	{
		_ranged = value;
	}
	
	public void setTwoWeaponPrimary(int value)
	{
		_twoWeaponPrimary = value;
	}
	
	public void setTwoWeaponSecondary(int value)
	{
		_twoWeaponSecondary = value;
	}
	
	public void setValue(String name, int melee, int ranged, int hands, int finessable, int light, int hitBonus, int twoWeaponPrimary, int twoWeaponSecondary, int attackSeparator, String damageBase, int damageBonus)
	{
		int type = ATTACK_TYPE_MELEE;
		if ((melee == 1) && (ranged == 1))
			type = ATTACK_TYPE_BOTH;
		else if (ranged == 1)
			type = ATTACK_TYPE_RANGED;
		
		super.setValue(hitBonus, type, ATTACK_TYPE_SECONDARY_NONE);
		
		setAttackSeparator(attackSeparator);
		
		setName(name);
		setMelee(melee);
		setRanged(ranged);
		setHands(hands);
		setLight(light);
		setTwoWeaponPrimary(twoWeaponPrimary);
		setTwoWeaponSecondary(twoWeaponPrimary);
		setDamage(damageBase);
		setMisc(damageBonus);
	}

	@Override
	protected void readFromParcelTransformEx(Parcel in)
		throws Throwable
	{
		try
		{
			super.readFromParcelTransformEx(in);

			_melee = in.readInt();
			_ranged = in.readInt();
			_light = in.readInt();
			_twoWeaponPrimary = in.readInt();
			_twoWeaponSecondary = in.readInt();
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransformEx", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransformEx(Parcel dest, int flags)
		throws Throwable
	{
		try
		{
			super.writeToParcelTransformEx(dest, flags);

			dest.writeInt(_melee);
			dest.writeInt(_ranged);
			dest.writeInt(_light);
			dest.writeInt(_twoWeaponPrimary);
			dest.writeInt(_twoWeaponSecondary);
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransformEx", tr);
    		throw tr;
    	}
	}	
	
	private int _melee;
	private int _ranged;
	private int _light;
	private int _twoWeaponPrimary;
	private int _twoWeaponSecondary;

	private static final String TAG = GameResourceWeapon.class.getSimpleName();
}
